package cn.edu.baiyunu.myapplication.ui.screens.add.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Backspace
import androidx.compose.material3.Icon
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
 * 金额输入键盘项目
 */
sealed class KeyboardItem {
    data class Number(val value: String) : KeyboardItem()
    object Decimal : KeyboardItem()
    object Delete : KeyboardItem()
}

/**
 * 自定义金额输入键盘
 */
@Composable
fun AmountKeyboard(
    onKeyPress: (KeyboardItem) -> Unit,
    modifier: Modifier = Modifier
) {
    // 创建键盘布局项
    val keyboardItems = listOf(
        KeyboardItem.Number("1"),
        KeyboardItem.Number("2"),
        KeyboardItem.Number("3"),
        KeyboardItem.Number("4"),
        KeyboardItem.Number("5"),
        KeyboardItem.Number("6"),
        KeyboardItem.Number("7"),
        KeyboardItem.Number("8"),
        KeyboardItem.Number("9"),
        KeyboardItem.Decimal,
        KeyboardItem.Number("0"),
        KeyboardItem.Delete
    )

    val haptic = LocalHapticFeedback.current

    Surface(
        modifier = modifier,
        color = Color(0xFF25252D),
        shadowElevation = 8.dp
    ) {
        LazyVerticalGrid(
            columns = GridCells.Fixed(3),
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp),
            verticalArrangement = Arrangement.spacedBy(8.dp),
            horizontalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            items(keyboardItems) { item ->
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(55.dp)
                        .clip(RoundedCornerShape(8.dp))
                        .background(Color.White.copy(alpha = 0.15f))
                        .clickable {
                            // 添加触觉反馈
                            haptic.performHapticFeedback(HapticFeedbackType.TextHandleMove)
                            onKeyPress(item)
                        },
                    contentAlignment = Alignment.Center
                ) {
                    when (item) {
                        is KeyboardItem.Number -> Text(
                            text = item.value,
                            color = Color.White,
                            fontSize = 22.sp,
                            fontWeight = FontWeight.Medium
                        )
                        is KeyboardItem.Decimal -> Text(
                            text = ".",
                            color = Color.White,
                            fontSize = 22.sp,
                            fontWeight = FontWeight.Bold
                        )
                        is KeyboardItem.Delete -> Icon(
                            imageVector = Icons.Default.Backspace,
                            contentDescription = "删除",
                            tint = Color.White,
                            modifier = Modifier.padding(4.dp)
                        )
                    }
                }
            }
        }
    }
} 